package com.wish.datastructure.hashtable;

/**
 * Created by suliucheng on 2021/4/19 13:19
 * Directions:
 */
public class StudentList {
    /**
     * 提供一个链表的头，初始为null，加入第一个数据的时候把该数据作为头。
     */
    private Student head;

    public void list() {
        if (head == null) {
            System.out.println("该链表上没有任何数据");
            return;
        }
        Student temp = head;
        while (true) {
            System.out.print(temp);
            if (temp.getNext() == null) {
                break;
            }
            temp = temp.getNext();
        }
    }

    /**
     * 新加一个学生信息
     *
     * @param student
     * @return
     */
    public Student add(Student student) {
        if (head == null) {
            head = student;
            return student;
        }
        Student temp = head;
        while (true) {
            if (temp.getNext() == null) {
                break;
            }
            temp = temp.getNext();
        }
        temp.setNext(student);
        return student;
    }


    public Student findById(int id) {
        Student temp = head;
        while (true) {
            if (temp == null) {
                break;
            } else if (temp.getId() == id) {
                return temp;
            }else {
                temp = temp.getNext();
            }
        }
        throw new RuntimeException("该id的信息不存在于所要查找的hash表上");
    }
}
